package com.qf.dao.impl;

import com.qf.dao.StudentQueryDao;
import com.qf.domain.Student;
import com.qf.utils.JDBCUtils2;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.List;

public class StudentQueryDaoImpl implements StudentQueryDao {
    private JdbcTemplate jd=new JdbcTemplate(JDBCUtils2.getDs());
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDs());
    @Override
    public List<Student> SelectAllStudent() {
        String sql="select * from ya";
        List<Student> query = jd.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
        return query;
    }

    @Override
    public long count() {
        try {
            long query = queryRunner.query("select count(id) from ya", new ScalarHandler<>());
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<Student> findALL(int page_cur, int page_size) {
        try {
            List<Student> query = queryRunner.query("select * from ya limit ?,?",
                    new BeanListHandler<>(Student.class),
                    (page_cur - 1) * page_size, page_size);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
